import axios from 'axios'

axios.defaults.baseURL = 'http://localhost:8888/api/private/v1'

axios.interceptors.request.use((config) => {
  let myToken = localStorage.getItem('mytoken') || ''

  if (myToken) {
    config.headers['Authorization'] = myToken
  }
  return config
},
(error) => {
  return Promise.reject(error)
})

export const handleLogin = (obj) => {
  return axios.post(`/login`, obj)
    .then(res => {
      return res.data
    })
}
export const userData = obj => axios.get('/users', { params: obj }).then(res => res.data)

export const delUser = id => axios.delete(`/users/${id}`).then(res => res.data)

export const addUser = obj => axios.post('/users', obj).then(res => res.data)

export const toggleState = obj => axios.put(`/users/${obj.id}/state/${obj.state}`).then(res => res.data)

export const editUser = obj => axios.put(`/users/${obj.id}`, obj).then(res => res.data)

export const roleList = () => axios.get(`/roles`).then(res => res.data)

export const roleChange = obj => axios.put(`/users/${obj.id}/role`, obj).then(res => res.data)

export const rightList = type => axios.get(`/rights/${type}`).then(res => res.data)

export const addRoles = obj => axios.post('/roles', obj).then(res => res.data)

export const deleteRole = id => axios.delete(`/roles/${id}`).then(res => res.data)

export const editRole = obj => axios.put(`/roles/${obj.id}`, obj).then(res => res.data)
